<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>支付配置</cite></a>
        <a><cite>支付接口</cite></a>
    </span>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">支付接口列表</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-row">
                    <div class="layui-form" style="float:right;">
                        <div class="layui-form-item" style="margin:0;">
                            <div class="layui-input-inline">
                                <input type="text" name="searchIfCode" id="searchIfCode" placeholder="接口代码" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="searchIfName" id="searchIfName" placeholder="接口名称" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <select name="searchPayType" id="searchPayType" lay-search="">
                                    <option value="">支付类型</option>
                                </select>
                            </div>
                            <div class="layui-input-inline">
                                <select name="type" id="searchStatus" placeholder="状态" lay-search="">
                                    <option value="-99">状态</option>
                                    <option value="0">关闭</option>
                                    <option value="1">开启</option>
                                </select>
                            </div>
                            <a id="search" class="layui-btn" data-type="reload"><i class="layui-icon layui-icon-search"></i>搜索</a>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layuiAdmin-btns" style="margin-bottom: 10px;">
                        <a class="layui-btn create"  data-events="create"><i class="layui-icon layui-icon-add-1"></i>新增支付接口</a>
                    </div>
                </div>
                <table class="layui-hide center" lay-filter="cList" id="cList"></table>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['table','util', 'form', 'admin'], function(){
        var table = layui.table
        ,$ = layui.$
        ,form = layui.form
        ,view = layui.view
        ,admin = layui.admin
        ,element = layui.element;

        element.render('breadcrumb', 'breadcrumb');//渲染导航信息

        //标题内容模板
        var tplTitle = function(d){
            return "<a class='layui-btn layui-btn-xs' lay-event='edit'><i class='layui-icon layui-icon-edit'></i>编辑</a>";
        };

        // 加载支付类型
        admin.req({
            type: 'post',
            url: layui.setter.baseUrl + '/config/pay_interface/getPayType',
            error: function(err){
                layer.alert(err);
            },
            success: function(res){
                if(res.code == 0){
                    var payTypeList = res.data;
                    //遍历赋值
                    for(var i in payTypeList){
                        $("#searchPayType").append('<option value= '+payTypeList[i].payType+'>'+payTypeList[i].payTypeName+'</option>');
                    }
                    form.render('select');
                }
            }
        });

        var tplStatus = function(d){
            var checkedHtml = " ifCode='"+d.ifCode+"' ";
            if(d.status == 1) {
                checkedHtml += " checked";
            }
            return '<input type="checkbox" lay-filter="statusSwitch" lay-skin="switch" '+checkedHtml+' lay-text="启用|停用">';
        };

        table.render({
            elem: '#cList'
            ,url: layui.setter.baseUrl + '/config/pay_interface/list'
            ,where: { access_token: layui.data(layui.setter.tableName).access_token}
            ,id: 'tableReload'
            ,cols: [[
                {field:'ifCode', title: '接口代码'}
                ,{field:'ifName', title: '接口名称'}
                ,{field:'ifTypeCode', title: '接口类型代码'}
                ,{field:'ifTypeName', title: '接口类型名称'}
                ,{field:'payTypeName',  title: '支付类型'}
                ,{field:'scene', title: '应用场景',templet: '<div>{{d.scene == 1?"移动APP":(d.scene == 2)?"移动网页":(d.scene == 3)?"PC网页":(d.scene == 4)?"微信公众平台":(d.scene == 5)?"手机扫码":""}}</div>'  }
                ,{field:'status', title: '状态',templet: tplStatus }
                ,{field:'edite', width: 100, fixed: 'right', title: '操作', templet: tplTitle }
            ]]
            ,page: true
            ,skin: 'line'
        });

        //监听工具条
        table.on('tool(cList)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                view.xxpayPopup("支付配置", "config/pay_interface/update", {ifCode: data.ifCode}, {area:['70%','80%']});
            }
        });

        $('.create').click(function(){
            view.xxpayPopup("新增支付接口", "config/pay_interface/create", {null: null}, {area:['70%','80%']});
        });

        form.on('switch(statusSwitch)', function(data){

            var ifCode = $(data.elem).attr('ifCode');
            var updateState = data.elem.checked ? "1" : "0";
            var loadingIndex = layer.load();
            admin.req({
                type: "POST",
                url: layui.setter.baseUrl + '/config/pay_interface/update',
                data: {ifCode: ifCode, status: updateState},
                success: function(res){
                    if(res.code == 0) {
                        table.reload('tableReload');
                    }
                },
                complete:function(){
                    layer.close(loadingIndex);
                }
            });
        });

        // 搜索
        var $ = layui.$, active = {
            reload: function(){
                var ifCode = $('#searchIfCode').val();
                var ifName = $('#searchIfName').val();
                var payType = $('#searchPayType').val();
                var status = $("#searchStatus").val();
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        ifCode: ifCode,
                        ifName: ifName,
                        payType: payType,
                        status: status
                    }
                });
            }
        };

        $('#search').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
            return false;
        });

        // 渲染表单
        form.render();

    });
    
</script>
